#!/bin/bash
# - rootfs
# - benchmark
# - pack_arch
# - os
# - os_arch
# - os_version

. $LKP_SRC/lib/debug.sh
. $LKP_SRC/lib/misc-base.sh
. $LKP_SRC/lib/reproduce-log.sh

[ -n "$benchmark" ] || die "benchmark is empty"
[ -n "$pack_arch" ] || die "pack_arch is empty"

DEPS_MNT=/osimage/deps
mkdir -p "$DEPS_MNT"

if 	[ -n "$rootfs" ]; then
	DISTRO=${rootfs%%-*}
	DISTRO=${DISTRO##*/}
	pack_to=$DEPS_MNT/$(basename $rootfs)

else
	[ -n "$os" ] || die "os is empty"
	[ -n "$os_arch" ] || die "os_arch is empty"
	[ -n "$os_version" ] || die "os_version is empty"
	DISTRO=${os}
	pack_to=$DEPS_MNT/${os}/${os_arch}/${os_version}

fi	

. $LKP_SRC/distro/${DISTRO}

mount $LKP_SERVER:$DEPS_MNT $DEPS_MNT || die "Failed to run mount"

umask 002

[[ -d "$pack_to" ]] || {
	mkdir -p "$pack_to"
	chown .lkp "$pack_to"
}

check_shared_package "$benchmark" && {
	original_benchmark=$benchmark
	benchmark=$BM_NAME
}

fixup_distro_mirror

update

fixup_preinstall

[[ "$benchmark" = "all" ]] && benchmark="$(ls $LKP_SRC/distro/depends | grep -v -e '-dev' -e '-allowlist')"

fixup_arch_install $pack_arch

pack_benchmark_deps

if [ -n "$original_benchmark" ]; then
	ln -sf "$pack_to/${BM_NAME}.cgz" "$pack_to/${original_benchmark}.cgz" || return
	echo "update shared pkg deps link ${original_benchmark}.cgz -> ${BM_NAME}.cgz"
fi
